package cg.studio.algorithm.collection;

import java.util.List;

public class Node<T>
{
  private T value;
  private Node<T> next;
  
  protected Node( T value )
  {
    this.value = value;
  }

  public Node( T[] array )
  {
    if( array == null || array.length == 0 )
      throw new IllegalArgumentException();
    
    value = array[0];
    Node<T> preNode = this;
    for( int index=1; index<array.length; ++index )
    {
      preNode.next = new Node<T>( array[index] );
      preNode = preNode.next;
    }

  }
  
  public Node( List<T> list, int startIndex )
  {
    value = list.get( startIndex );
    next = new Node<T>( list, ++startIndex );
  }
  
  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    sb.append( value.toString() );
    Node<T> node = next;
    while( node != null )
    {
      sb.append( "->" ).append( node.value );
      node = node.next;
    }
    return sb.toString();
  }

  public T getValue()
  {
    return value;
  }

  public void setValue( T value )
  {
    this.value = value;
  }

  public Node< T > getNext()
  {
    return next;
  }

  public void setNext( Node< T > next )
  {
    this.next = next;
  }
  
  
}
